<?php
class Gift_model extends CI_Model{

	function record_list($limit = 0, $offset = 0, $conditions = array()){
		$where = "1 = 1 ";
		if(!empty($conditions['child_id'])){
			$where .= " AND gr.child_id = '{$conditions['child_id']}' ";
		}
		$sql = "SELECT SQL_CALC_FOUND_ROWS gr.id,gr.status,FROM_UNIXTIME(gr.add_time, '%Y-%m-%d %H:%i:%S') AS add_time,gr.update_time,gr.stars,g.name,g.image FROM {$this->db->dbprefix}gift_record AS gr ";
		$sql .= "LEFT JOIN {$this->db->dbprefix}gifts AS g ON(g.id=gr.gift_id) ";
		$sql .= "WHERE $where ORDER BY gr.id DESC LIMIT $limit, $offset";
		$result = $this->db->query($sql)->result_array();
		$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
		return array("count" => $row_num['row_num'], "list" => $result);
	}

	function get_record_info($record_id){
		$sql = "SELECT gr.id,gr.status,FROM_UNIXTIME(gr.add_time, '%Y-%m-%d %H:%i:%S') AS add_time,FROM_UNIXTIME(gr.update_time, '%Y-%m-%d %H:%i:%S') AS update_time,gr.stars,g.name,g.image FROM {$this->db->dbprefix}gift_record AS gr ";
		$sql .= "LEFT JOIN {$this->db->dbprefix}gifts AS g ON(g.id=gr.gift_id) WHERE gr.id='{$record_id}'";
		$result = $this->db->query($sql)->row_array();
		return $result;
	}
}